import axios from 'axios';


/**
 * 浏览器中下载blob
 * 依赖window, document
 * @param {Blob} blob 二进制流
 * @param {String} name 文件名
 */
function downloadBlob(blob, name = 'file.xlsx') {
    const blobUrl = URL.createObjectURL(blob);

    // Create a link element
    const link = document.createElement('a');

    // Set link's href to point to the Blob URL
    link.href = blobUrl;
    link.download = name;

    // Append link to the body
    document.body.appendChild(link);

    // Dispatch click event on the link
    // This is necessary as link.click() does not work on the latest firefox
    link.dispatchEvent(
        new MouseEvent('click', {
            bubbles: true,
            cancelable: true,
            view: window
        })
    );

    // Remove link from body
    document.body.removeChild(link);

    window.URL.revokeObjectURL(link);
}


const downloadInstance = axios.create({
    // timeout: 3000,
    retry: 3,
    retryDelay: 1000,
    withCredentials: true,
    responseType: 'blob'
});

export {
    downloadBlob,
    downloadInstance
};
